import axios from "axios";
import {Message} from 'element-plus';
// import { getToken } from './auth'
const instance = axios.create({
  baseURL: '',
  timeout: 5000
})
// sessionStorage.setItem('token', response.data.token);
instance.interceptors.request.use(config => {
    const token = sessionStorage.getItem('token');
    // 如果token存在，将其添加到请求头中
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config
  },
  error => {
    console.log(error);
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(response => {
    const res = response.data
    if(res.status !== 200) {
      // Message.error(res.message || 'Error')
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res.data
    }
  },
  error => {
    console.log('err' + error)
    // Message.error(error.message)
    return Promise.reject(error)
  }
)

export default instance